# 在线学生信息管理
from mylib.stu_handler import StudentDB

# 实例化学员信息操作类
stuDB=StudentDB()

# 查询所有学员信息
def getAllStudentData():
    '''查询所有学员信息'''
    studentList = stuDB.findAll()
    for s in studentList:
        stu_id=s[0]
        stu_name=s[1]
        stu_age = s[2]
        stu_sex = s[3]
        stu_class = s[4]
        print("|{0:<4}|{1:{5}<10}|{2:<4}|{3:<4}|{4:<8}".format(stu_id,stu_name,stu_age,stu_sex,stu_class,chr(12288)))

# 添加学生信息
def addStudent():
    ''' 添加学生信息'''
    stu_name = input("请输入要添加学生姓名：")
    while True:# 输入年龄，并转换为int类型
        try:
            stu_age = input("请输入要添加学生年龄：")
            stu_age=int(stu_age)
            break
        except ValueError as err:
            print("年龄格式错误，请重新输入：")
    stu_sex = input("请输入要添加学生性别：")
    stu_class = input("请输入要添加学生班级：")
    newStudent=(stu_name,stu_age,stu_sex,stu_class,)
    result=stuDB.insert(newStudent)
    if result>0:
        print("成功添加学生信息！")

# 删除学生信息
def delStudent():
    '''删除学生信息'''
    while True:# 输入学生，并转换为int类型
        try:
            stu_id = input("请输入要删除学生的ID：")
            stu_id=int(stu_id)
            break
        except ValueError as err:
            print("学生的ID格式错误，请重新输入：")
    result=stuDB.delStudentById(stu_id)
    if result>0:
        print("成功删除学生信息！")

while True:
    # 输出初始界面
    print("\n","=" * 10, "学员管理系统", "=" * 10)
    print("{:2}{:12}{:12}".format(" ","1.查看学员信息", "2.添加学生信息"))
    print("{:2}{:12}{:12}".format(" ","3.删除学生信息", "4.退出系统"))
    print("=" * 34)
    # 根据用户输入，执行对应的操作
    key = input("请输入您的选择：")
    if key=="1":
        print("="*8,"查看学员信息","="*8)
        getAllStudentData()
    elif key=="2":
        print("="*8,"添加学生信息","="*8)
        addStudent()
    elif key=="3":
        print("="*8,"删除学生信息","="*8)
        delStudent()
    elif key=="4":
        print("="*8,"退出系统","="*8)
        break
    else:
        print("=" * 8, "无效的选择", "=" * 8)


